/*
 * Copyright (C) 2010-2011 x264 project
 *
 * Authors: Steven Walters <kemuri9@gmail.com>
 *          Pegasys Inc. <http://www.pegasys-inc.com>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * WinRT threads to pthreads wrapper
 */

#ifndef FFMPEG_COMPAT_WRTPTHREADS_H
#define FFMPEG_COMPAT_WRTPTHREADS_H

/* Build up a pthread-like API using underlying Windows RunTime API. Have only static
 * methods so as to not conflict with a potentially linked in pthread-winrt
 * library.
 * As most functions here are used without checking return values,
 * only implement return values as necessary. */

#define WIN32_LEAN_AND_MEAN
#include <Synchapi.h>
#include <windows.h>
//#define inline __inline
#include "libavutil/attributes.h"
#include "libavutil/common.h"
#include "libavutil/internal.h"
#include "libavutil/mem.h"

#include "wRTpthreadsTypeDef.h" 

/* the conditional variable api for windows 6.0+ uses critical sections and
 * not mutexes */
typedef CRITICAL_SECTION pthread_mutex_t;
typedef CONDITION_VARIABLE pthread_cond_t;


#define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)

static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
{
	InitializeCriticalSection(m);
	return 0;
}
static inline int pthread_mutex_destroy(pthread_mutex_t *m)
{
    DeleteCriticalSection(m);
    return 0;
}
static inline int pthread_mutex_lock(pthread_mutex_t *m)
{
    EnterCriticalSection(m);
    return 0;
}
static inline int pthread_mutex_unlock(pthread_mutex_t *m)
{
    LeaveCriticalSection(m);
    return 0;
}
static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
{
    InitializeConditionVariable(cond);
    return 0;
}

/* native condition variables do not destroy */
static inline void pthread_cond_destroy(pthread_cond_t *cond)
{
    return;
}

static inline void pthread_cond_broadcast(pthread_cond_t *cond)
{
    WakeAllConditionVariable(cond);
}

static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
    SleepConditionVariableCS(cond, mutex, INFINITE);
    return 0;
}

static inline void pthread_cond_signal(pthread_cond_t *cond)
{
    WakeConditionVariable(cond);
}
static av_unused void w32thread_init(void)
{

}

#endif /* FFMPEG_COMPAT_WRTPTHREADS_H */
